package com.paddyi.controller;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.paddyi.commons.http.HttpResult;
import com.paddyi.commons.page.MyPage;
import com.paddyi.model.Document;
import com.paddyi.service.IDocumentService;

/**
 *  (Document)
 * @author paddy
 * @since  2019-08-05 06:40:35
 */
@RestController
@RequestMapping("document")
public class DocumentController{

	@Autowired
	private IDocumentService documentService;

	/**
	 * 保存
	 * @param record
	 * @return
	 */	
	@PostMapping(value="/add")
	public HttpResult add(@RequestBody Document record) {
		record.setCreateTime(new Date());
		record.setUpdateTime(new Date());
		record.setStatus(0);
		return HttpResult.ok(documentService.save(record));
	}

    /**
     * 删除
     * @param records
     * @return
     */
	@PostMapping(value="/del")
	public HttpResult del(@RequestBody List<Long> records) {
		return HttpResult.ok(documentService.removeByIds(records));
	}

	/**
     * 修改
     *
     * @param record
     * @return
     */
    @PostMapping(value = "/edit")
    public HttpResult edit(@RequestBody Document record) {
        return HttpResult.ok(documentService.updateById(record));
    }

    /**
     * 基础分页查询,条件查询只支持等于，如果想要更高级的功能，比如大于小于等等，请自己写吧
     * @param page
     * @return
     */    
	@PostMapping(value="/findPage")
	public HttpResult findPage(@RequestBody MyPage<Document> page) {
	    if(page.getQuerycondition()!=null){
            Document document = page.getQuerycondition().get(0);
            QueryWrapper<Document> queryWrapper = new QueryWrapper<>();
            for(Field f:document.getClass().getDeclaredFields()){
                f.setAccessible(true);
                try {
                    if(f.get(document) != null && !"serialVersionUID".equals(f.getName())){
                        queryWrapper.eq(f.getName(), f.get(document));
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            return HttpResult.ok(documentService.page(page,queryWrapper));
        }
		return HttpResult.ok(documentService.page(page));
	}
	
    /**
     * 根据主键查询
     * @param id
     * @return
     */ 	
	@GetMapping(value="/find")
	public HttpResult find(@RequestParam Long id) {
		return HttpResult.ok(documentService.getById(id));
	}

	/**
	 * 根据所有
	 * @return
	 */
	@GetMapping(value="/findAll")
	public HttpResult findAll() {
		QueryWrapper queryWrapper = new QueryWrapper();
		queryWrapper.orderByAsc("seq");
		List docList = documentService.list(queryWrapper);
		return HttpResult.ok(docList);
	}
}
